;(function() {
	if(getcookie('user')!= undefined){ 
	if(getcookie('good') != undefined) {
		$('.order-pay').addClass('hide');
		var goodlist = JSON.parse(getcookie('good'));
		for(let q = 0; q < goodlist.length; q++) {
			$.ajax({
				type: "post",
				async: false,
				url: "../php/cart.php",
				data: "type=lie&id=" + goodlist[q].gid,
				success: function(d) {
					d = JSON.parse(d);
					d[2] = parseFloat(d[2].substring(1)).toFixed(2); //数据库中取出的价格
					var gcount = parseInt(goodlist[q].gcount); //数量
					var h = '<li class="pre-sell-box tractive"><div class="pay-pro td" style="width: 368px;"><input name="goods" type="checkbox"checked=""class="cart-item-checked"/><a class="pay-pro-icon" href="xiangqing.html?id=' + goodlist[q].gid + '" target="_blank"><img src="' + d[1] + '"></a><p class="pay-pro-info"><a href="xiangqing.html?id="' + goodlist[q].gid + '" target="_blank" data-role="item-title">' + d[0] + '</a><em class="pay-pro-detail"><span><b title="青灰色">颜色：青灰色</b>尺码：' + goodlist[q].gsize + '<i class="iconfont"></i></span></em></p></div><div class="product-price td " style="width:148px;"><p class="p-product-price">¥' + d[2] + '</p></div><div style="width:128px;" class="adjust-cart-num td"><div class="cart-num-cont"><span class="jian cart-num-btn"><i>-</i></span><input type="text" value="' + gcount + '" readonly="readonly"><span class="cart-num-btn jia" style="position: absolute;top: 0;left:64px"><i>+</i></span></div><p class="tip-message "></p></div><div style="width:160px;" class="sub-total red td">¥' + (d[2] * gcount).toFixed(2) + '</div><div style="width:100px;" class="cart-operation td"><span class="cart-del-btn" data-role="cart-del-btn">删除</span><span class="cart-col-btn" data-role="cart-mov2fav-btn">移入收藏</span></div></li>';
					$('.table-group').append(h);
				}
			});
			$.ajax({
				type: "post",
				url: "../php/cart.php",
				data: "type=sl&size=" + goodlist[q].gsize,
				async: false,
				success: function(d) {
					d = JSON.parse(d);
					var $this = $('.table-group li').eq(q);
					$this.data('count', d[1]);
					$this.data('size', d[0]);
					$this.data('gtype', goodlist[q].gtype);
				}
			});
		};
		quicks();
		$('.btn-account').click(function() {
			alert($('.btn-account').data('go'));
			if($('.btn-account').data('go')) {
				alert(1);
				let p = $('p.sum strong').text();
				let c = $('.ins').text();
				$('.forsure .box span').eq(0).text(c);
				$('.forsure .box span').eq(1).text(p);
				$('.domain strong').text(p);
				$('.forsure').removeClass('hide');
			}
		})
		$('.head span').click(function() {
			$('.jisuan').addClass("hide");
			location.href = 'car.html';
			return false;
		});
		$('.gongxi input').click(function() {
			$('.jisuan').addClass("hide");
			location.href = 'index.html';
			return false;
		});
		$('.forsure input').eq(0).click(function() {
			$('.forsure').addClass('hide');
			delcookie('good');
			$('.jisuan').removeClass('hide');
		})
		$('.forsure input').eq(1).click(function() {
			$('.forsure').addClass('hide');
		})
		$('input[name="goods"]').click(function() {
			var b = 1;
			$.each($('input[name="goods"]'), function() {
				if(!($(this).is(':checked'))) {
					b = 0;
				}
			});
			if(b) {
				$('input[name="all"]').prop('checked', 'checked');
			} else {
				$('input[name="all"]').prop('checked', false);
			}
			quicks();
		})
		$('.delete-all-sel').click(function() {
			$('input[name="goods"]').each(function(i) {
				if($(this).is(':checked')) {
					//						alert($(this).index());
					$('span.cart-del-btn').eq(i).click();
				}
			})
		})

		$('input[name="all"]').click(function() {
			if($(this).is(':checked')) {
				$('input[name="goods"]').prop('checked', 'checked');
			} else {
				$('input[name="goods"]').prop('checked', false);
			}
			quicks();
		})
	} else {
		$('.mm').addClass('hide');
	}

	function quicks() {
		var price = 0;
		var sum = 0;
		if($('.cart-num-cont').length <= 0) {
			$('.mm').addClass('hide');
			$('.order-pay').removeClass('hide');
			delcookie('good');
		} else {
			for(let i = 0; i < $('.cart-num-cont').length; i++) {
				var dj = Number($('.cart-num-cont').eq(i).find('input').val());
				var pc = Number($('.p-product-price').eq(i).text().substring(1));
				$('.sub-total').eq(i).html("¥" + dj * pc);
				if($('input[name="goods"]').eq(i).is(':checked')) {
					sum += Number($('.cart-num-cont').eq(i).find('input').val());
					price += Number($('.sub-total').eq(i).text().substring(1));
				}
			}
			$('.sum strong').html("¥ " + price);
			$('.select-num strong').html(sum);
			if(price >= 299) {
				$('input[name="by"]').prop('checked', 'checked')
			} else {
				$('input[name="by"]').prop('checked', false)
			}
		}
		if(price == 0) {
			$('.btn-account').css({
				'background': 'gray',
				'cursor': 'default'
			})
			$('.btn-account').data('go', 0);
		} else {
			$('.btn-account').css({
				'background': '#d0021b',
				'cursor': 'pointer'
			})
			$('.btn-account').data('go', 1);
		}
	}
	$('.tipsbox a').click(function() {
		$('.tipsbox').addClass('hide');
	})
	$('.jia').click(function() {
		let shu = $('.cart-num-cont').eq($(this).index('.jia')).find('input');
		let pd = $('.table-group li').eq($(this).index('.jia'));
		if(shu.val() < Number(pd.data('count'))) {
			shu.val(Number(shu.val()) + 1);
			goodlist[$(this).index('.jia')].gcount = Number(goodlist[$(this).index('.jia')].gcount) + 1;
			setcookie('good', JSON.stringify(goodlist), 7);
			jjpan($(this).index('.jia'), shu, pd);
			quicks();
		}
	})
	$('.jian').click(function() {
		let shu = $('.cart-num-cont').eq($(this).index('.jian')).find('input');
		let pd = $('.table-group li').eq($(this).index('.jia'));
		if(shu.val() != '1') {
			shu.val(Number(shu.val()) - 1);
			goodlist[$(this).index('.jian')].gcount = Number(goodlist[$(this).index('.jian')].gcount) - 1;
			setcookie('good', JSON.stringify(goodlist), 7);
			jjpan($(this).index('.jian'), shu, pd);
			quicks();
		}
	})
	$('span.cart-del-btn').click(function() {
		var index = $(this).index('.cart-del-btn');
		$('.table-group li').eq(index).remove();
		goodlist.splice(index, 1);
		setcookie('good', JSON.stringify(goodlist), 7);
		quicks();
	});

	function jjpan(q, shu, pd) {
		if(shu.val() == '1') {
			$('.jian').eq(q).addClass('disable');
		} else {
			$('.jian').eq(q).removeClass('disable');
		}
		if(pd) {
			var s = Number(shu.val());
			var k = Number(pd.data('count'));
			if(s >= k) {
				$('.jia').eq(q).addClass('disable');
			} else {
				$('.jia').eq(q).removeClass('disable');
			}
		}
	}
	}else{
		alert('非法访问,请先登录');
		location.href='login.html';
	}
})();